﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmProductBackEnd : Form
    {

        string realLocationSN;
        string EndLocationSn;
        int SNStatus=0;
        public FrmProductBackEnd()
        {
            InitializeComponent();
        }

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(cmb_BillNo.Text))
            {

                MessageBox.Show("请先输入需要退捡的单号!");
                return;
            }
            BindCMBBoxCodeEnd(cmb_BillNo.Text);
        }

        DataTable DtOut;
        DataTable DtOutEndBoxCode;
        DataTable RowDetailDT;
        DataTable DtInventoryEnd;
        DataTable DtInventory;

        public void InputBillNo()
        {
            DtOut = Bll_ProductOut.GetEndOutStockLists(cmb_BillNo.Text);
            DataRow[] drOut = DtOut.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drOut.Length > 0)
            {
                DataTable info = Bll_ProductOut.GetOutStockInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {
                        var msg = GetMessage();
                        lab_msg.Text = cmb_BillNo.Text + "详情：" + msg;
                        cmb_BoxCodeEnd.Focus();
                    }
                    else
                    {
                        MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        cmb_BillNo.Text = "";
                        cmb_BillNo.Focus();
                    }
                }

            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "单号不正确,请输入正确的单据!";
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();
            }
        }

        private void FrmProductBack_Load(object sender, EventArgs e)
        {
            //BindCMB();
        }

        //public void BindCMB()
        //{
        //    DtOut = Bll_ProductOut.GetEndOutStockList();
        //    cmb_BillNo.Items.Clear();
        //    if (DtOut.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in DtOut.Rows)
        //        {
        //            cmb_BillNo.Items.Add(dr["BillNo"].ToString());
        //        }
        //    }
        //}

        public void BindCMBBoxCodeEnd(string BillNo)
        {
             DtOutEndBoxCode = Bll_ProductOut.GetEndStockBoxCodeList(BillNo);
            cmb_BoxCodeEnd.Items.Clear();
            if (DtOutEndBoxCode.Rows.Count > 0)
            {
                foreach (DataRow dr in DtOutEndBoxCode.Rows)
                {
                    cmb_BoxCodeEnd.Items.Add(dr["BoxNo"].ToString());
                }
            }
            var lab_text = GetMessage();
            lab_msg.Text = cmb_BillNo.Text + "详情：" + lab_text;
            cmb_BoxCodeEnd.Focus();

        }

        public string GetMessage()
        {
            var dt = Bll_ProductOut.GetDeliveryInfo(cmb_BillNo.Text);
            var result = string.Empty;

            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    var selectQty = 0;
                    if (!string.IsNullOrEmpty(dr["SelectQty"].ToString()))
                    {
                        selectQty =Convert.ToInt32(dr["SelectQty"]);
                    }
                    var planQty = 0;
                    if (!string.IsNullOrEmpty(dr["PlanQty"].ToString()))
                    {
                        planQty = Convert.ToInt32(dr["PlanQty"]);
                    }

                    sb.AppendLine("物料号:" + dr["ProductNumber"] + ";计划数量:" + planQty + ";实际数量:" + selectQty);
                }
                result = sb.ToString();

            }
            return result;
        }

        private void txt_BoxCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(cmb_BillNo.Text))
                {
                    
                    MessageBox.Show("请先输入需要退捡的单号!");
                    return;
                }

                if (string.IsNullOrEmpty(cmb_BoxCodeEnd.Text))
                {
                     
                    MessageBox.Show("请先输入需要退捡的箱号!");
                    return;
                }


                if (string.IsNullOrEmpty(txt_BoxCode.Text))
                {
                     
                    MessageBox.Show("请先输入需要使用的箱号!");
                    return;
                }

                var splitList = txt_BoxCode.Text.Split('-');
                var boxNo = splitList[0];
                txt_BoxCode.Text = boxNo;

                RowDetailDT = Bll_ProductOut.GetOutStockRowDetailsInfo(cmb_BillNo.Text, cmb_BoxCodeEnd.Text);

                if (RowDetailDT.Rows.Count == 0)
                {
                    //lab_msg.Text = txt_BoxCode.Text + "不正确或不属于此单!";
                    MessageBox.Show(cmb_BoxCodeEnd.Text + "不正确或不属于此单!");
                    cmb_BoxCodeEnd.Text = "";
                    cmb_BoxCodeEnd.Focus();
                    return;
                }

                DataRow[] drOutEndDeatil = DtOutEndBoxCode.Select(string.Format(" BoxNo = '{0}'", cmb_BoxCodeEnd.Text));

                if (DtInventory.Rows.Count > 0)
                {
                    if (Convert.ToInt32(drOutEndDeatil[0]["Qty"]) >Convert.ToInt32(DtInventory.Rows[0]["Qty"]))
                    {
                        MessageBox.Show(txt_LocationSN.Text + "库位库存数量不足，请检查之后重新输入库位！");
                        txt_LocationSN.Text = "";
                        txt_LocationSN.Focus();
                        return;
                    }
                    else if (drOutEndDeatil[0]["PartNumber"].ToString() != DtInventory.Rows[0]["PartNumber"].ToString())
                    {
                        MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号物料号与该库位" + drOutEndDeatil[0]["PartNumber"].ToString() + "物料号不符!");
                        txt_LocationSN.Text = "";
                        txt_LocationSN.Focus();
                        return;
                    }
                    else if (drOutEndDeatil[0]["Version"].ToString() != DtInventory.Rows[0]["Version"].ToString())
                    {
                        MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号版本与该库位" + drOutEndDeatil[0]["Version"].ToString() + "版本不符!");
                        txt_LocationSN.Text = "";
                        txt_LocationSN.Focus();
                        return;
                    } 
                }
                else
                {
                    MessageBox.Show(txt_LocationSN.Text + "库位没有库存，请检查之后重新输入库位！");
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                    return;
                }

                var array = realLocationSN.Split('#');
                var storageSN = array[0];
                var areaSN = array[0] + "#" + array[1];

                var arrayEnd = EndLocationSn.Split('#');
                var storageSNEnd = arrayEnd[0];
                var areaSNEnd = arrayEnd[0] + "#" + arrayEnd[1];

                if (RowDetailDT.Rows.Count > 0) //如果退捡的箱号和订单号存在
                {
                     
                    bool backRes = false;
                    
                    backRes = Bll_ProductOut.BackOutStockRowDetailsEndInventory(RowDetailDT, storageSN, areaSN, realLocationSN, storageSNEnd,areaSNEnd,EndLocationSn, txt_BoxCode.Text.Trim());
                     
                  
                    if (backRes)
                    {
                        lab_msg.Text = "箱号:" + txt_BoxCode.Text + "料号:" + RowDetailDT.Rows[0]["PartNumber"].ToString() + "数量" + RowDetailDT.Rows[0]["Qty"].ToString() + "退捡成功。\r\n退捡之后" + cmb_BillNo.Text + "详情：" + GetMessage();
                        txt_BoxCode.Focus();
                        txt_BoxCode.Text = "";
                    }
                    else
                    {
                        //lab_msg.Text = txt_BoxCode.Text + "退捡失败";
                        MessageBox.Show(txt_BoxCode.Text + "退捡失败,请重试！");
                        txt_BoxCode.Focus();
                        txt_BoxCode.Text = "";
                    }

                }
                else
                {
                    //lab_msg.Text = txt_BoxCode.Text + "不正确或不属于此单!";
                    MessageBox.Show(txt_BoxCode.Text + "不正确或不属于此单!");
                    txt_BoxCode.Focus();
                    txt_BoxCode.Text = "";
                }


            }
        }



        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(cmb_BillNo.Text))
                {

                    MessageBox.Show("请先输入需要退捡的单号!");
                    return;
                }
                BindCMBBoxCodeEnd(cmb_BillNo.Text);
            }
        }

        private void cmb_BoxCodeEnd_SelectedIndexChanged(object sender, EventArgs e)
        {
            OutStockEndBoxVerification();
        }

        /// <summary>
        /// 选择推荐单号时
        /// </summary>
        public void OutStockEndBoxVerification()
        {

            var splitList = cmb_BoxCodeEnd.Text.Split('-');
            var boxNo = splitList.FirstOrDefault();
            cmb_BoxCodeEnd.Text = boxNo;

            DataRow[] drOutEndDeatil = DtOutEndBoxCode.Select(string.Format(" BoxNo = '{0}'", cmb_BoxCodeEnd.Text));
            if (drOutEndDeatil.Length > 0)
            {
                //EndLocationSn = drOutEndDeatil[0]["Location_SN"].ToString();
                //SNStatus = Bll_ProductIn.GetLocationSNStatus(EndLocationSn);
                //if (SNStatus == 1)//如果该库位在使用
                //{
                //    DtInventoryEnd = Bll_ProductOut.GetInventoryeList(EndLocationSn);
                //    if (DtInventoryEnd.Rows.Count > 0) //如果该库位在使用中的情况下 库存表有纪录
                //    {
                //        if (drOutEndDeatil[0]["PartNumber"].ToString() != DtInventoryEnd.Rows[0]["PartNumber"].ToString())
                //       {
                //            MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号物料号与该库位" + EndLocationSn + "现有物料号不符!");
                //            cmb_BoxCodeEnd.Text = "";
                //            cmb_BoxCodeEnd.Focus();
                //            return;
                //       }
                //        else if (drOutEndDeatil[0]["Version"].ToString() != DtInventoryEnd.Rows[0]["Version"].ToString())
                //       {
                //            MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号版本与该库位"+EndLocationSn+"现有版本不符!");
                //            cmb_BoxCodeEnd.Text = "";
                //            cmb_BoxCodeEnd.Focus();
                //            return;
                //       }
                //       else
                //       {
                //           txt_LocationSN.Focus();
                //        }
                //    }
                //    else //如果该库位在使用中的情况下 但库存表没有纪录
                //    {
                //        MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号不正确,请输入正确的箱号!");
                //        cmb_BoxCodeEnd.Text = "";
                //        cmb_BoxCodeEnd.Focus();
                //        return;
                //    }
                //}
                //else if (SNStatus == 0) //如果该库位没有使用
                //{
                //    txt_LocationSN.Focus();
                //}
                //else 
                //{
                //    MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号不正确,请输入正确的箱号!");
                //    cmb_BoxCodeEnd.Text = "";
                //    cmb_BoxCodeEnd.Focus();
                //    return;
                //}
                txt_LocationSN.Focus();
            }
            else
            {
                lab_msg.Text = cmb_BoxCodeEnd.Text + "箱号不正确,请输入正确的箱号!";
                cmb_BoxCodeEnd.Text = "";
                cmb_BoxCodeEnd.Focus();
                return;
            }
        }

        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;

            }
            realLocationSN = txt_LocationSN.Text;
            if (string.IsNullOrEmpty(cmb_BoxCodeEnd.Text))
            {
                MessageBox.Show("箱号不能为空!");
                return;
            }

            if (string.IsNullOrEmpty(realLocationSN))
            {
                MessageBox.Show("库位不能为空!");
                return;
            }

            var array = realLocationSN.Split('#');

            if (array.Count() < 3)
            {
                MessageBox.Show("库位格式不正确，请检查确认!");
                return;
            }

            var storageSN = array[0];
            var areaSN = array[0] + "#" + array[1];
            txt_LocationSN.Text = array[1] + array[2];
            DataRow[] drOutEndDeatil = DtOutEndBoxCode.Select(string.Format(" BoxNo = '{0}'", cmb_BoxCodeEnd.Text));
            DtInventory = Bll_ProductOut.GetInventoryeList(realLocationSN);
            EndLocationSn = drOutEndDeatil[0]["Location_SN"].ToString();
            if (DtInventory.Rows.Count > 0)
            {
                if (Convert.ToDecimal(drOutEndDeatil[0]["Qty"]) > Convert.ToDecimal(DtInventory.Rows[0]["Qty"])) 
                {
                    MessageBox.Show(txt_LocationSN.Text + "库位库存数量不足，请检查之后重新输入库位！");
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                    return;
                }
                else if (drOutEndDeatil[0]["PartNumber"].ToString() != DtInventory.Rows[0]["PartNumber"].ToString())
                {
                    MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号物料号与该库位" + drOutEndDeatil[0]["PartNumber"].ToString() + "物料号不符!");
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                    return;
                }
                else if (drOutEndDeatil[0]["Version"].ToString() != DtInventory.Rows[0]["Version"].ToString())
                {
                    MessageBox.Show(cmb_BoxCodeEnd.Text + "箱号版本与该库位" + drOutEndDeatil[0]["Version"].ToString() + "版本不符!");
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                    return;
                }
                else
                {
                    txt_BoxCode.Focus();
                }
            }
            else 
            {
                MessageBox.Show(txt_LocationSN.Text + "库位没有库存，请检查之后重新输入库位！");
                txt_LocationSN.Text = "";
                txt_LocationSN.Focus();
                return;
            }
             

        }

        private void cmb_BoxCodeEnd_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == 13)
            {
                OutStockEndBoxVerification();
            }
        }

       

    }
}